package com.example.bysj_sb.service;

import com.example.bysj_sb.dao.StudentDao;
import com.example.bysj_sb.domain.Degree;
import com.example.bysj_sb.domain.Student;
import com.example.bysj_sb.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("studentService")
//@Transactional注解：可以用在方法上和类上，用在类上代表这个类的所有公共非静态方法都将启用事务功能。
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation. REQUIRED)
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private UserService userService;
    //添加
    public Boolean add(Student student){
        boolean added = false;
        //添加student同时添加user
        User user = new User(student.getNo(), student.getNo(), null, student);
        userService.add(user);
        added=studentDao.add(student);
        return added;
    }
    //根据id查找
    public Student find(int id){
        Student student = studentDao.find(id);
        return student;
    }
    //查找全部
    public List<Student> findAll(){
        List<Student> students = studentDao.findAll();
        return students;
    }
    //修改
    public Boolean update(Student student){
        return studentDao.update(student);
    }
    //删除
    public Boolean delete(int id){
        return studentDao.delete(id);
    }
}
